package com.example.article.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.article.dao.ApArticleConfigMapper;
import com.example.article.dao.ApArticleContentMapper;
import com.example.article.dao.ApArticleMapper;
import com.example.article.dao.ApAuthorMapper;
import com.example.article.service.ArticleService;
import com.usian.model.article.dtos.ApArticleDto;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.article.pojos.ApArticleConfig;
import com.usian.model.article.pojos.ApArticleContent;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.dtos.WmNewsDtos;
import com.usian.utils.common.SnowflakeIdWorker;
import com.usian.utils.common.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;
    @Autowired
    private ApArticleMapper apArticleMapper;
    @Autowired
    private ApArticleContentMapper apArticleContentMapper;
    @Autowired
    private ApAuthorMapper apAuthorMapper;
    @Value("${fastdfs.url}")
    private String prefixUrl;

    @Override
    public ResponseResult getAricle(ApArticleDto apArticleDto) {
        apArticleDto.setSize(10);
        LambdaQueryWrapper<ApArticle> queryWrapper = new LambdaQueryWrapper<>();
        if (apArticleDto.getChannelId()!=null){
            queryWrapper.eq(ApArticle::getChannelId,apArticleDto.getChannelId());
        }
        if (apArticleDto.getFlag()!=null){
            queryWrapper.eq(ApArticle::getFlag,apArticleDto.getFlag());
        }
        Page<ApArticle> page = new Page<>(1,apArticleDto.getSize());
        switch (apArticleDto.getType()){
            case "1":{
                queryWrapper.ge(ApArticle::getPublishTime,apArticleDto.getMaxBehotTime());
                break;
            }
            case "2":{
                queryWrapper.le(ApArticle::getPublishTime,apArticleDto.getMinBehotTime());
                break;
            }
            default:{
                break;
            }
        }
        IPage<ApArticle> apArticleIPage = apArticleMapper.selectPage(page, queryWrapper);
        return ResponseResult.okResult(apArticleIPage.getRecords());
    }

    @Override
    public ResponseResult getArticleById(long id) {
        LambdaQueryWrapper<ApArticleConfig> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApArticleConfig::getArticleId,id);
        ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(queryWrapper);
        if (apArticleConfig.getIsDown()==0 && apArticleConfig.getIsDelete()==0){
            return ResponseResult.okResult(apArticleConfig);
        }
        return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
    }

    @Override
    public ApAuthor getApAuthor(long authorId) {
        return apAuthorMapper.selectById(authorId);
    }


    @Override
    public Integer addArticle(WmNewsDtos wmNewsDtos) {
        ApArticle apArticle = new ApArticle();
        apArticle.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
        apArticle.setTitle(wmNewsDtos.getTitle());
        LambdaQueryWrapper<ApAuthor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ApAuthor::getName,wmNewsDtos.getName());
        ApAuthor apAuthor = apAuthorMapper.selectOne(queryWrapper);
        if(apAuthor==null){
            apAuthor.setName(wmNewsDtos.getName());
            apAuthor.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
            apAuthor.setType(2);
            apAuthor.setCreatedTime(new Date());
            apAuthorMapper.insert(apAuthor);
        }
        apArticle.setAuthorId(apAuthor.getId());
        apArticle.setAuthorName(apAuthor.getName());
        apArticle.setChannelId(wmNewsDtos.getChannelId());
        apArticle.setChannelName(wmNewsDtos.getChannelName());
        apArticle.setFlag(0);
        apArticle.setLabels(wmNewsDtos.getLabels());
        apArticle.setLayout(3);
        StringBuilder imageList = new StringBuilder();
        if (!wmNewsDtos.getContent().equals("")){
            List<Map> maps = JSONArray.parseArray(wmNewsDtos.getContent(), Map.class);
            for (Map map1 : maps) {
                if (map1.get("type").equals("image")){
                    String value = prefixUrl+StringUtils.substringPath((String) map1.get("value"));
                    imageList.append(value);
                }
            }
        }
        apArticle.setImages(imageList.toString());
        apArticle.setCreatedTime(new Date());
        apArticle.setPublishTime(wmNewsDtos.getPublishTime());
        apArticle.setSyncStatus(0);
        apArticle.setOrigin(0);
        apArticleMapper.insert(apArticle);
        ApArticleConfig apArticleConfig = new ApArticleConfig();
        apArticleConfig.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
        apArticleConfig.setArticleId(apArticle.getId());
        apArticleConfig.setIsComment(1);
        apArticleConfig.setIsForward(1);
        apArticleConfig.setIsDown(0);
        apArticleConfig.setIsDelete(0);
        apArticleConfigMapper.insert(apArticleConfig);
        ApArticleContent apArticleContent = new ApArticleContent();
        apArticleContent.setId(Integer.parseInt((SnowflakeIdWorker.UUID()+"").substring(8,13)));
        apArticleContent.setArticleId(apArticle.getId());
        apArticleContent.setContent(wmNewsDtos.getContent());
        apArticleContentMapper.insert(apArticleContent);
        return Integer.parseInt(apArticle.getId()+"");
    }
}
